<template>
	<view class="wrap">
		<view class="info-wrap">
			<view class="card-info">
				<view class="flex">
					<image :src="cardInfo.image" mode=""></image>
					<view class="text-info">
						<view class="name">{{cardInfo.bankName}}</view>
						<view class="type">储蓄卡</view>
					</view>
				</view>
				<view class="number">**** **** **** **** {{sliceBank(cardInfo.cardNo)}}</view>
			</view>
			<view class="info-text">
				<view class="item">
					<view class="left">当前卡号</view>
					<view class="right">
						<text>{{formatBank(cardInfo.cardNo)}}</text>
						<text class="btn" @click="copy(cardInfo.cardNo)">复制</text>
					</view>
				</view>
				<view class="item">
					<view class="left">开户人姓名</view>
					<view class="right">
						<text>{{driverName}}</text>
					</view>
				</view>
				<view class="item">
					<view class="left">开户人身份证</view>
					<view class="right">
						<text>{{userInfo.idcard}}</text>
					</view>
				</view>
				<view class="item">
					<view class="left">开户人手机</view>
					<view class="right">
						<text>{{userInfo.mobile}}</text>
					</view>
				</view>
				<view class="item">
					<view class="left">开户时间</view>
					<view class="right">
						<text>{{cardInfo.creationTime}}</text>
					</view>
				</view>
			</view>
		</view>
		<view class="list" v-if="cardInfo.type == 1">
			<view style="padding: 12rpx;">默认提现卡</view>
			<view>
				<switch :checked="cardInfo.isDefault == 1" color="#177CF1" @change="switchChange" style="transform:scale(0.7)"/>
			</view>
		</view>
		<view class="list" @click="goBill">
			<view style="padding: 12rpx;">交易记录</view>
			<image src="@/static/icon_enter.png" mode=""></image>
		</view>
		<view class="unbind" @click="unbindCard" v-if="cardInfo.type == 1">解除绑定</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				cardInfo: null,
				userInfo: uni.getStorageSync(this.DATA_KEY.userInfo),
				driverName: uni.getStorageSync(this.DATA_KEY.userInfo).driverName.replace(/[\r\n]/g, "")
			}
		},
		onLoad(options) {
			console.log(uni.getStorageSync(this.DATA_KEY.userInfo).driverName.replace(/[\r\n]/g, ""))
			this.cardInfo = JSON.parse(options.info)
			if(this.cardInfo.creationTime) this.cardInfo.creationTime = this.timestampToTime(this.cardInfo.creationTime)
		},
		methods: {
			unbindCard() {
				uni.showModal({
					title: '提醒',
					content: '是否要将该卡解除绑定？',
					cancelColor: '#999',
					success: res => {
						if (res.confirm) {
							this.wxRequest({
								url: `${this.API}/jzbBank/unbindRelateAcct`,
								method: 'POST',
								title: '加载中...',
								data: {
									tranNetMemberCode: uni.getStorageSync(this.DATA_KEY.userInfo).driverId,
									subAcctNo: this.cardInfo.jzbSubAccountNo,
									memberAcctNo: this.cardInfo.cardNo,
								},
								success: res => {
									console.log(res.data.data.TxnReturnCode == '000000')
									if(res.data.data.TxnReturnCode == '000000'){
										uni.showToast({
											title: '解绑成功',
											icon: 'none',
											duration: 2000
										});
									}
									uni.navigateBack()
								}
							});
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			copy(num) {
				uni.setClipboardData({
					data: num,
					success: () => {
						console.log('复制成功');
					}
				});
				
			},
			sliceBank(bank) {
				// return bank.replace(/[\s]/g, 'string').replace(/(\d{4})(?=\d)/g, "$1 ")
				return bank.slice(-4)
			},
			formatBank(bank) {
				return bank.replace(/[\s]/g, 'string').replace(/(\d{4})(?=\d)/g, "$1 ")
			},
			goBill() {
				uni.navigateTo({
					url: `./bill?bankNo=${this.cardInfo.cardNo}`
				})
			},
			timestampToTime(timestamp) {
			  var  date =  new  Date(timestamp);
			  let Y = date.getFullYear() +  '-' ;
			  let M = (date.getMonth()+1 < 10 ?  '0' +(date.getMonth()+1) : date.getMonth()+1) +  '-' ;
			  let D = (date.getDate()+1 < 10 ?  '0' +(date.getDate()+1) : date.getDate()+1) +  ' ' ;
			  // let D = date.getDate() +  ' ' ;
			  let h = date.getHours() +  ':' ;
			  let m = date.getMinutes() +  ':' ;
			  let s = date.getSeconds();
			  return  Y+M+D;
			  // return  Y+M+D+h+m+s;
			},
			switchChange(e) {
				console.log(e.detail.value)
				this.wxRequest({
					url: `${this.API}/myWallet/updateIsDefault`,
					method: 'POST',
					title: '加载中...',
					data: {
						cardNo: this.cardInfo.cardNo,
						type: 1,
						isDefault: e.detail.value ? 1 : 0,
					},
					success: res => {
						console.log(res)
						uni.showToast({
							title: '操作成功',
							icon: 'none',
							duration: 2000
						});
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.unbind{
		font-size: 28rpx;
		font-weight: 500;
		color: #FF063A;
		text-align: center;
		padding: 32rpx;
		margin-top: 16rpx;
		background: #fff;
	}
	.list{
		margin-top: 16rpx;
		background: #fff;
		padding: 24rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 28rpx;
		font-weight: 500;
		color: #333333;
		image{
			width: 28rpx;
			height: 28rpx;
		}
	}
	.info-wrap{
		padding: 24rpx;
		background: #fff;
		.info-text{
			margin-top: 24rpx;
			.item{
				display: flex;
				justify-content: space-between;
				align-items: center;
				font-size: 28rpx;
				padding: 24rpx 0;
				border-bottom: 1rpx solid #F9F9F9;
				&:last-child{
					border-bottom: none;
				}
				.left{
					font-weight: 500;
					color: #333333;
				}
				.right{
					font-weight: 400;
					color: #A4A4A4;
					.btn{
						color: #177CF1;
						margin-left: 16rpx;
					}
				}
			}
		}
	}
	.card-info {
		padding: 24rpx;
		box-shadow: 0 0 20rpx 8rpx rgba(0, 0, 0, 0.04);
		border-radius: 8rpx;
		background-image: linear-gradient(to right, #3993FF, #036EE8);
		color: #fff;
	
		.flex {
			display: flex;
			justify-content: center;
			align-items: center;
	
			image {
				width: 60rpx;
				height: 60rpx;
				background: #fff;
				border-radius: 20%;
				padding: 5rpx;
			}
	
			.text-info {
				flex: 1;
				margin: 0 24rpx;
	
				.name {
					font-size: 28rpx;
				}
	
				.type {
					font-size: 22rpx;
					margin-top: 4rpx;
				}
			}
		}
	
		.number {
			padding-left: 84rpx;
			font-size: 44rpx;
			font-weight: 500;
			margin-top: 24rpx;
		}
	}
</style>